set mbox_type   = Maildir
set sendmail    = /usr/bin/msmtp

set folder      = ~/Mail/Gmail/
set spoolfile   = "+INBOX"
set mbox        = "+archive"
set postponed   = "+drafts"
unset record

mailboxes \
  "+INBOX" \
  "+starred" \
  "+task" \
  "+CR" \
  "+ML.wikitech-l" \
  "+ML.mediawiki-l" \
  "+ML.wmfall" \
  "+ML.ops" \
  "+ML.github" \
  "+ML.debian-security" \
  "+ML.debian-user" \
  "+ML.debian-devel" \
  "+ML.debian-bugs" \
  "+ML.coreboot" \
  "+ML.unicode" \
  "+CI" \
  "+pipelinebot" \
  "+auto" \
  "+sent" \
  "+drafts" \
  "+archive" \
  "+spam" \
  "+trash"

# main options
set realname   = "Tyler Cipriani"
# set from       = "tyler@tylercipriani.com"
#
send-hook . "set from = tyler@tylercipriani.com"
send-hook "~t @wikimedia.org" "set from = tcipriani@wikimedia.org"
send-hook "~t @lists.wikimedia.org" "set from = tcipriani@wikimedia.org"

set mail_check = 5
set envelope_from

unset move           # gmail does that
set delete           # don't ask, just do
unset confirmappend  # don't ask, just do!
set quit             # don't ask, just do!!
unset mark_old       # read/new is good enough for me

# https://www.ietf.org/rfc/rfc2646.txt
# http://www.mdlerch.com/emailing-mutt-and-vim-advanced-config.html
set text_flowed

# sort/threading
set sort     = threads
set sort_aux = reverse-date-received
# Too pedantic maybe
# set sort_re

# look and feel
set pager_index_lines = 8
set pager_context     = 5
set pager_stop
set menu_scroll
set smart_wrap
set tilde
unset markers
set date_format="%y-%m-%d %T"
set status_format = "-%r- %f%?t? Tagged:%t?%?n? New:%n? -%>-"

# sidebar (requires patched mutt-sidebar)
set sidebar_visible = yes
set sidebar_width = 38
# set sidebar_delim='│'
set mail_check_stats=yes
set sidebar_visible=yes
set sidebar_format="%B%?F? [%F]?%* %?N?%N/?%S"
# set sidebar_format="%B%*  %S%?N?(%N)?%?F?[%F]?"
set sidebar_indent_string=" "
set sidebar_folder_indent=yes
set sidebar_short_path=yes


# composing
set fcc_attach
unset mime_forward
set forward_format = "Fwd: %s"
set include
set forward_quote

ignore *                               # first, ignore all headers
unignore from: to: cc: date: subject:  # then, show only these
hdr_order from: to: cc: date: subject: # and in this order

set beep_new   # bell on new mail

# GPG sign
source ~/.mutt/gpgrc

set pgp_timeout = 3600       # how long to cache the pass-phrase
set crypt_autosign = yes     # automatically sign all outgoing mail
set crypt_replyencrypt = yes # automatically encrypt replies to encrypted messages
set pgp_sign_as = 018FAC02   # my Key ID
set pgp_use_gpg_agent = yes
my_hdr X-PGP-Key: https://tylercipriani.com/018FAC02.asc

# color
source "$HOME/.mutt/colors"
source "$HOME/.mutt/subscribe"

# caching for faster search
set header_cache     = "~/Mail/Cache"
set message_cachedir = "~/Mail/Cache"

# attachments
set mailcap_path = "~/.mutt/mailcap"
bind attach <return> view-mailcap

bind index,pager \CP sidebar-prev       # Ctrl-Shift-P - Previous Mailbox
bind index,pager \CN sidebar-next       # Ctrl-Shift-N - Next Mailbox
bind index,pager \CO sidebar-open       # Ctrl-Shift-O - Open Highlighted Mailbox

# Fix backspce in urxvt
bind index,pager  previous-line

# Contacts
set query_command="goobook query %s"
macro index,pager a \
    "<pipe-message>goobook add<return>" "add sender to google contacts"
bind editor <Tab> complete-query

bind index,pager R  group-reply

# Shortcuts

source "$HOME/.mutt/vim-keys.rc"

macro index ,t '<enter-command>toggle sidebar_visible<enter><refresh>'
macro pager ,t '<enter-command>toggle sidebar_visible<enter><redraw-screen>'
macro pager ,u "|urlview<enter>" "call urlview to open links"

macro index gi "<change-folder>!<return>" "go to inbox"
macro index gs "<change-folder> =starred<return>" "go to starred"
macro index ,z "<shell-escape>offlineimap -q -o<return>" "sync IMAP"

macro pager ,g "|~/.mutt/view-web.py \
    --trim '<>' --ltrim 'X-Gerrit-ChangeURL:' 'X-Gerrit-ChangeURL'<enter>" \
    "Open gerrit link in browser"

macro pager ,p ":set pipe_decode<enter>|~/.mutt/view-web.py \
    'https://phabricator.wikimedia.org/'<enter> \
    :unset pipe_decode<enter>" \
    "Open phab link in browser"

macro index <space> "<collapse-thread>" "uncollapse thread"

macro index ,r \
    "<enter-command>set my_old_pipe_decode=\$pipe_decode my_old_wait_key=\$wait_key nopipe_decode nowait_key<enter>\
    <pipe-message>notmuch-mutt -r thread<enter>\
    <change-folder-readonly>`echo ${XDG_CACHE_HOME:-$HOME/.cache}/notmuch/mutt/results`<enter>\
    <enter-command>set pipe_decode=\$my_old_pipe_decode wait_key=\$my_old_wait_key<enter>" \
        "notmuch: reconstruct thread"

macro index ,s \
    "<enter-command>set my_old_pipe_decode=\$pipe_decode my_old_wait_key=\$wait_key nopipe_decode nowait_key<enter>\
    <shell-escape>notmuch-mutt -r --prompt search<enter>\
    <change-folder-readonly>`echo ${XDG_CACHE_HOME:-$HOME/.cache}/notmuch/mutt/results`<enter>\
    <enter-command>set pipe_decode=\$my_old_pipe_decode wait_key=\$my_old_wait_key<enter>" \
        "notmuch: search mail"

macro pager ,v \
    "<copy-message>/tmp/mutttmpbox\n<enter> \
    <shell-escape>~/.mutt/view_html_email.py<enter>" \
    "View HTML in browser"

macro pager ,l "|~/.mutt/unsub.py<enter>" \
      "Find unsub header and open in browser"

macro index A "<save-message>=archive\n<sync-mailbox>"

# Sanely handle multi-part messages (prefer text part, but yield to
# mailcap-configured rendering for html-only)
# http://terminalmage.net/2014/03/16/how-i-read-html-email-with-mutt.html
alternative_order text/plain text/html
auto_view text/html
